package com.liruo.learn.spring.security.controller;

import com.liruo.learn.spring.security.auth.AuthenticUser;
import com.liruo.learn.spring.security.model.LoginReq;
import com.liruo.learn.spring.security.resphandler.BaseResp;
import com.liruo.learn.spring.security.resphandler.BusinessException;
import com.liruo.learn.spring.security.user.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * @Author:liruo
 * @Date:2023-05-26-19:44:06
 * @Desc
 */
@Slf4j
@RestController
@RequestMapping("/auth")
public class AuthController {
    @PostMapping("/login")
    public BaseResp<AuthenticUser> login(@RequestBody LoginReq loginReq) {
        log.info("loginReq={}", loginReq);
        final String account = loginReq.getAccount();
        final String credential = loginReq.getCredential();

        switch (loginReq.getAccountType()) {
            case email:
                if (!"2372221537@qq.com".equals(account)) {
                    throw new BusinessException("email must 2372221537@qq.com");
                }
                break;
            case phone:
                if (!"12345678901".equals(account)) {
                    throw new BusinessException("phone must 12345678901");
                }
                break;
        }
        switch (loginReq.getCredentialType()) {
            case password:
                if (!"123456".equals(credential)) {
                    throw new BusinessException("password must 123456");
                }
                break;
            case verificationCode:
                if (!"123456".equals(credential)) {
                    throw new BusinessException("verificationCode must 123456");
                }
                break;
        }
        return BaseResp.ok(new AuthenticUser(new User(1L,"李若", "", "2372221537@qq.com", "12345678901")));
    }
}
